package com.lht.cozyCloud.bee.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.lht.cozyCloud.bee.domain.MessageRecord;
import com.lht.cozyCloud.bee.service.AsyncMessageService;
import com.lht.cozyCloud.bee.service.MessageRecordService;
import com.lht.cozyCloud.bee.service.MsmService;
import com.lht.cozyCloud.bee.utils.Properties;
import com.lht.cozyCloud.bee.utils.RequestUtil;
import com.lht.cozyCloud.bee.vo.MsmVo;
import com.lht.cozyCloud.common.core.annotation.Excel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author lht
 * @version 1.0
 * @description: TODO
 * @date 2024/11/8 10:43
 */

@Service
public class AliyunMsmServiceImpl implements MsmService {

    @Autowired
    private IAcsClient aliClient;

//    @Autowired
//    private MessageRecordService messageRecordService;
    @Autowired
    private AsyncMessageService asyncMessageService;

    @Override
    public boolean sendMsm(String phone, String code, String template) {
        // 校验空
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            return false;
        }
        HashMap<String, Object> param = new HashMap<>();
        param.put("code", code);
        CommonRequest aliyunRequest = RequestUtil.aliyunRequest(phone, param);
        //调用阿里云进行短信发送
        try {
            CommonResponse response = aliClient.getCommonResponse(aliyunRequest);;
            System.out.println(response.getData());
            LocalTime now = LocalTime.now();
            MessageRecord record = new MessageRecord();
            record.setType(1);
            record.setContent("");
            record.setAccount(phone);
            record.setTemplateType(template);
            record.setSystemType("-");
            record.setObjectType(1);
            record.setSendType(1);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String now_str = now.format(formatter);
            record.setSendTime(now_str);
            if (response.getHttpResponse().isSuccess()) {
                record.setStatus(1);
                asyncMessageService.insertMessageRecord(record);
                return true;
            }else {
                asyncMessageService.insertMessageRecord(record);
                record.setStatus(0);
                return false;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean send(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())) {
            String code = (String) msmVo.getParam().get("code");
            return this.sendMsm(msmVo.getPhone(), code, msmVo.getTemplateCode());
        }
        return false;
    }
}
